package com.woniu.inlet.timer;

import com.alibaba.fastjson.JSON;
import com.woniu.adapter.FixedCurrentIncomeAdapter;
import com.woniu.adapter.ProductSellAdapter;
import com.woniu.inlet.web.vo.FixedDayVo;
import com.woniu.outlet.client.FinancePayFeign;
import com.woniu.outlet.client.FinanceUsercenterFeign;
import com.woniu.outlet.client.SendOrderMsgToPayFeign;
import com.woniu.outlet.client.mq.FixedSellPayMQ;
import com.woniu.outlet.mysql.po.FixedCurrentIncomeMySQL;
import com.woniu.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定期产品
 * 每日定时任务：定期产品对持有天数进行更新，达到日期自动卖出
 */
@Component
@Slf4j
@Transactional
public class FixedIncomeTimer {

    @Autowired
    private FixedCurrentIncomeAdapter incomeAdapter;

    @Autowired
    private ProductSellAdapter sellAdapter;

    @Autowired
    private SendOrderMsgToPayFeign sendOrderMsgToPayFeign;

    /**
     * "0 1 0 ? * *"
     * 定期产品对持有天数进行更新，达到日期自动卖出
     */
    @Scheduled(cron ="0 1 0 ? * *") //每天0:01 更新持有天数  每隔10秒执行一次-->"*/10 * * * * ?"
    public void fixedHoldDay() {
        log.debug("定期产品更新持有天数开始，开始时间{}",DateUtil.getBuyTime());
        List<FixedCurrentIncomeMySQL> allFixed = incomeAdapter.getAllFixed();
        //得到今日日期
        String todayDate = DateUtil.getSendDate();
        int count=0;
        for (FixedCurrentIncomeMySQL f : allFixed) {
            //如果今日日期与买入日期一致，则不进行追加
            if(todayDate.equals(f.getStartDate())) continue;;
            //判断定期时长与持有天数是否一致。一致就自动卖出
            if(f.getFixedDay()-f.getHoldDay()==0){
                sellAdapter.fixedAndCurrentSell(f);
                //修改状态为1，已卖出
                incomeAdapter.autoSellFixed(f);
                //通知平台收益-,由平台再通知个人中心，用户余额+
                sendMsgToPay(f);
                log.debug("发送卖出消息通知平台收益-");
                count++;
                log.debug("id=[{}]持有天数满足，卖出产品",f.getId());
            }else{
                f.setHoldDay(f.getHoldDay()+1);
                incomeAdapter.updateById(f);
                log.debug("id=[{}]持有天数不满足，持有天数+1={}",f.getId(),f.getHoldDay());
            }
        }
        log.debug("定期产品更新持有天数结束，共有{}个产品到期卖出",count);
    }

    public void sendMsgToPay(FixedCurrentIncomeMySQL mySQL){
        FixedDayVo vo=new FixedDayVo();
        BeanUtils.copyProperties(mySQL,vo);
        int i=1;
        while (i<=5){
            try {
                sendOrderMsgToPayFeign.sendMessage(FixedSellPayMQ.EXCHANGE,FixedSellPayMQ.ROUTING_KEY,JSON.toJSONString(vo));
                log.debug("定期消息发送成功");
                incomeAdapter.deleteByPrimaryKey(mySQL.getId());
                log.debug("定期产品卖出，删除单子");
                break;
            } catch (Exception e) {
                log.debug("定期消息发送失败，重试");
                if(i==5){
                    log.debug("定期单子id={}卖出失败,打电话解决",mySQL.getId());
                    //e.printStackTrace();
                }
                i++;
            }
        }
    }
}
